package com.yuan.weike.controller;

import com.yuan.weike.common.BaseResponse;
import com.yuan.weike.common.ResultUtils;
import com.yuan.weike.exception.BusinessException;
import com.yuan.weike.exception.ErrorCode;
import com.yuan.weike.model.TaskEvaluation;
import com.yuan.weike.model.User;
import com.yuan.weike.service.TaskEvaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/task-evaluation")
public class TaskEvaluationController {

    @Autowired
    private TaskEvaluationService taskEvaluationService;

    /**
     * 添加任务评价
     *
     * @param taskEvaluation 任务评价对象
     * @param request HTTP请求
     * @return 添加后的任务评价对象
     */
    @PostMapping("/add")
    public BaseResponse<TaskEvaluation> addTaskEvaluation(@RequestBody TaskEvaluation taskEvaluation, HttpServletRequest request) {
        // 验证参数
        if (taskEvaluation == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (taskEvaluation.getTaskId() == null || taskEvaluation.getTaskId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "任务ID无效");
        }
        if (taskEvaluation.getEmployerId() == null || taskEvaluation.getEmployerId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "雇主ID无效");
        }
        if (taskEvaluation.getWitkeyId() == null || taskEvaluation.getWitkeyId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "威客ID无效");
        }
        if (taskEvaluation.getScore() == null || taskEvaluation.getScore() < 1 || taskEvaluation.getScore() > 5) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "评分应在1-5之间");
        }
        if (taskEvaluation.getContent() == null || taskEvaluation.getContent().isEmpty()) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "评价内容不能为空");
        }

        // 从session中获取当前用户
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }

        // 只有管理员可以添加任务评价
        if (loginUser.getRole() != 0) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "无权限添加任务评价");
        }

        TaskEvaluation result = taskEvaluationService.addTaskEvaluation(taskEvaluation);
        return ResultUtils.success(result);
    }

    /**
     * 根据ID获取任务评价
     *
     * @param evaluationId 评价ID
     * @param request HTTP请求
     * @return 任务评价对象
     */
    @GetMapping("/get/{evaluationId}")
    public BaseResponse<TaskEvaluation> getTaskEvaluationById(@PathVariable Long evaluationId, HttpServletRequest request) {
        // 检查评价ID是否有效
        if (evaluationId == null || evaluationId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "评价ID无效");
        }

        // 从session中获取当前用户
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }

        TaskEvaluation taskEvaluation = taskEvaluationService.getTaskEvaluationById(evaluationId);
        return ResultUtils.success(taskEvaluation);
    }

    /**
     * 获取所有任务评价
     *
     * @param request HTTP请求
     * @return 任务评价列表
     */
    @GetMapping("/list")
    public BaseResponse<List<TaskEvaluation>> listTaskEvaluations(HttpServletRequest request) {
        // 从session中获取当前用户
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }

        // 只有管理员可以查看所有任务评价
        if (loginUser.getRole() != 0) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "无权限查看任务评价");
        }

        List<TaskEvaluation> taskEvaluations = taskEvaluationService.getAllTaskEvaluations();
        return ResultUtils.success(taskEvaluations);
    }

    /**
     * 更新任务评价
     *
     * @param taskEvaluation 任务评价对象
     * @param request HTTP请求
     * @return 更新后的任务评价对象
     */
    @PutMapping("/update")
    public BaseResponse<TaskEvaluation> updateTaskEvaluation(@RequestBody TaskEvaluation taskEvaluation, HttpServletRequest request) {
        // 验证参数
        if (taskEvaluation == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (taskEvaluation.getEvaluationId() == null || taskEvaluation.getEvaluationId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "评价ID无效");
        }
        if (taskEvaluation.getScore() != null && (taskEvaluation.getScore() < 1 || taskEvaluation.getScore() > 5)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "评分应在1-5之间");
        }

        // 从session中获取当前用户
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }

        // 只有管理员可以更新任务评价
        if (loginUser.getRole() != 0) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "无权限更新任务评价");
        }

        TaskEvaluation result = taskEvaluationService.updateTaskEvaluation(taskEvaluation);
        return ResultUtils.success(result);
    }

    /**
     * 删除任务评价
     *
     * @param evaluationId 评价ID
     * @param request HTTP请求
     * @return 是否删除成功
     */
    @DeleteMapping("/delete/{evaluationId}")
    public BaseResponse<Boolean> deleteTaskEvaluation(@PathVariable Long evaluationId, HttpServletRequest request) {
        // 检查评价ID是否有效
        if (evaluationId == null || evaluationId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "评价ID无效");
        }

        // 从session中获取当前用户
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }

        // 只有管理员可以删除任务评价
        if (loginUser.getRole() != 0) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "无权限删除任务评价");
        }

        boolean result = taskEvaluationService.deleteTaskEvaluation(evaluationId);
        return ResultUtils.success(result);
    }
}